﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Xml.Linq;
using System.Threading.Tasks;
using System.Threading;
using RestSharp;

namespace PushNotificationsHelper.WindowsApp
{
    public class ToastNotification
    {
        public XDocument XmlTemplate { get; private set; }
        public ToastTemplate ChosenTemplate { get; private set; }
        public string SecretKey { get; private set; }
        public string Sid { get; private set; }
        
        private ToastNotification(string key, string sid) 
        {
            SecretKey = key;
            Sid = sid;
        }

        public static ToastNotification CreateToastNotification(string key, string sid)
        {
            return new ToastNotification(key, sid);
        }

        public ToastNotification UsingTemplate(ToastTemplate template)
        {
            ChosenTemplate = template;
            XmlTemplate = ToastXmlTemplateFactory.GetXDocumentForToastTemplate(template);
            return this;
        }

        public ToastNotification WithText(List<string> positionalText)
        {
            var textNodes = XmlTemplate.Descendants(XName.Get("text"));

            foreach (var item in positionalText)
            {
                var node = (from t in textNodes
                            let att = t.Attributes(XName.Get("id")).FirstOrDefault()
                            where att != null && att.Value == (positionalText.IndexOf(item)+1).ToString()
                            select t).FirstOrDefault();
                node.SetValue(item);                            
            }
            return this;
        }

        public ToastNotification WithImages(Dictionary<int, string> imagesDictionary)
        {
            Contract.Requires<ArgumentNullException>(imagesDictionary != null);
            foreach (var item in imagesDictionary)
            {
                var textNodes = XmlTemplate.Descendants(XName.Get("Image"));
            }
            return this;
        }

        public bool SendPushNotification(string uri)
        {
            string notificationType = "wns/toast";
            string textOfNotification = XmlTemplate.ToString();
            var accessToken = WNSAuthHelper.GetAccessToken(SecretKey, Sid);
            
            
            var client = new RestClient(uri);
            var request = new RestRequest(Method.POST)
                .AddHeader("X-WNS-Type", notificationType)
                .AddHeader("Authorization", String.Format("Bearer {0}", accessToken.AccessToken))
                .AddParameter("text/xml", XmlTemplate.ToString(), ParameterType.RequestBody);
            var response = client.Execute(request);
            System.Diagnostics.Debug.WriteLine(response.Content);
            System.Diagnostics.Debug.WriteLine(response.ErrorMessage);
            System.Diagnostics.Debug.WriteLine(response.ResponseStatus);
            return response.StatusCode.Equals("OK");
        }
    }
}
